clear,clc
numIter =10;
conf = 3;
confidence_percent = conf;

path_origdata = '/mri_projects/ASD_Analysis/DataProcessed/rest';
ListPath = '/mri_projects/ASD_Analysis/Lists';
outputDIR = '/mri_projects/ASD_Analysis/Results/fc_mats';

SIDs = textread([ListPath '/variability_list_ASD_GU.txt'],'%s');

tmp_hemi_Data = load_surf_file(path_origdata, SIDs{1}, 'lh');
[row, col] = size(tmp_hemi_Data);
res_lh_corr = [];
res_rh_corr = [];
%%%%%%%%%%%%%%%%%%%%

for s = 1:length(SIDs)  
    fprintf(['[step]: ' num2str(s) ' start \n']);
    
    sid = SIDs{s};

    Files_LH = dir([path_origdata '/' sid '/surf/lh' '.*sm6_fs4.nii.gz']);
    lh_filename_bold = [Files_LH(1).name];
    fullfilename_bold = [path_origdata '/'  sid '/surf/' lh_filename_bold];
    lh_hdr = MRIread(fullfilename_bold);
    lhData_raw = reshape(lh_hdr.vol,[size(lh_hdr.vol,1)*size(lh_hdr.vol,2)*size(lh_hdr.vol,3), size(lh_hdr.vol,4)]);

    Files_RH = dir([path_origdata '/' sid '/surf/rh' '.*sm6_fs4.nii.gz']);
    rh_filename_bold = [Files_RH(1).name];
    fullfilename_bold = [path_origdata '/'  sid '/surf/' rh_filename_bold];
    rh_hdr = MRIread(fullfilename_bold);
    rhData_raw = reshape(rh_hdr.vol,[size(rh_hdr.vol,1)*size(rh_hdr.vol,2)*size(rh_hdr.vol,3), size(rh_hdr.vol,4)]);

    whData_raw = [lhData_raw; rhData_raw];
    whData_raw(isnan(whData_raw)) = 0.0;
    whData_raw = whData_raw';

    [fc, p] = corrcoef(whData_raw);
    fc(isnan(fc)) = 0.0;
    
    save([outputDIR '/GU/' sid '_fc.mat'], 'fc');

    clear p fc lhData_raw rhData_raw;

    fprintf(['[step]: ' num2str(s) ' finished \n']);
end
